package xiaoka.ecarx.com.cn.youlong.player;

import android.app.Notification;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.AlbumList;
import com.ximalaya.ting.android.opensdk.model.track.CommonTrackList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.player.advertis.MiniPlayer;
import com.ximalaya.ting.android.opensdk.player.service.IXmPlayerStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayerConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhanghao on 2016/11/1.
 */

public class ClientPlayerManager {

    private static final String TAG = ClientPlayerManager.class.getName();

    private static byte[] sLock = new byte[0];

    protected XmPlayerManager mPlayerManager;
    private CommonRequest mXimalaya;

    private static volatile ClientPlayerManager sInstance;

    private Context context;

    private PlayerDataProvider playerDataProvider;

    private Notification mNotification;

    private String mAppSecret = "f90f31b71b4cc1275e9af2ed9060bd13";

    public static ClientPlayerManager getInstance(Context appCtx) {
        if (sInstance == null) {
            byte[] var1 = sLock;
            synchronized (sLock) {
                if (sInstance == null) {
                    sInstance = new ClientPlayerManager(appCtx);
                }
            }
        }

        return sInstance;
    }

    private ClientPlayerManager(Context context) {
        this.context = context;
    }

    public void init() {
        XmPlayerConfig.getInstance(context).setSDKHandleAudioFocus(false);
        XmPlayerConfig.getInstance(context).setSDKHandlePhoneComeAudioFocus(false);
        XmPlayerConfig.getInstance(context).setSDKHandleHeadsetPlugAudioFocus(false);

        mXimalaya = CommonRequest.getInstanse();
        mXimalaya.init(context, mAppSecret);

        mPlayerManager = XmPlayerManager.getInstance(context);
        mPlayerManager.setPlayMode(XmPlayListControl.PlayMode.PLAY_MODEL_LIST_LOOP);

        mPlayerManager.init();
//        mNotification = XmNotificationCreater.getInstanse(context).initNotification(context, MainActivity.class);
//        mPlayerManager.init((int) System.currentTimeMillis(), mNotification);
        mPlayerManager.setOnConnectedListerner(new XmPlayerManager.IConnectListener() {
            @Override
            public void onConnected() {
                mXimalaya.setDefaultPagesize(50);
                Toast.makeText(context, "播放器初始化成功", Toast.LENGTH_SHORT).show();
            }
        });

        mPlayerManager.setPlayMode(XmPlayListControl.PlayMode.PLAY_MODEL_LIST_LOOP);//默认设置列表循环

        Log.d(TAG, "init playermanager");
        playerDataProvider = PlayerDataProvider.getInstance();

    }

    public void addPlayerStatusListener(IXmPlayerStatusListener iXmPlayerStatusListener){
        mPlayerManager.addPlayerStatusListener(iXmPlayerStatusListener);
    }

    public void removePlayerStatusListener(IXmPlayerStatusListener iXmPlayerStatusListener){
        mPlayerManager.removePlayerStatusListener(iXmPlayerStatusListener);
    }

    /**
     * 播放当前歌曲
     */
    public void play() {
        mPlayerManager.play();
    }

    /**
     * 播放第index首track
     *
     * @param index
     */
    public void play(int index) {
        mPlayerManager.play(index);
    }

    /**
     * 暂停当前歌曲
     */
    public void pause() {
        mPlayerManager.pause();
    }

    /**
     * 播放上一首
     */
    public void playPre() {
        mPlayerManager.playPre();
    }

    /**
     * 播放下一首
     */
    public void playNext() {
        mPlayerManager.playNext();
    }

    /**
     * 是否有下一首
     *
     * @return
     */
    public boolean hasPreSound() {
        return mPlayerManager.hasPreSound();
    }

    /**
     * 是否有上一首
     *
     * @return
     */
    public boolean hasNextSound() {
        return mPlayerManager.hasNextSound();
    }

    /**
     * 拿到当前播放某一首歌的位置
     *
     * @return
     */
    public int getPlayCurrPositon() {
        return mPlayerManager.getPlayCurrPositon();
    }

    /**
     * 拿到当前播放的一首歌的总时间
     *
     * @return
     */
    public int getDuration() {
        return mPlayerManager.getDuration();
    }

    /**
     * 定位到某一首歌的某个时间
     *
     * @param ms
     */
    public void seekTo(int ms) {
        mPlayerManager.seekTo(ms);
    }

    /**
     * 是否当前正在播放
     *
     * @return
     */
    public boolean isPlaying() {
        return mPlayerManager.isPlaying();
    }

    /**
     * 停止播放
     */
    public void stop() {
        mPlayerManager.stop();
    }

    /**
     * 设置播放器的播放模式
     *
     * @param mode
     */
    public void setPlayMode(XmPlayListControl.PlayMode mode) {
        mPlayerManager.setPlayMode(mode);
    }

    /**
     * 根据index拿到某个track
     *
     * @param index
     * @return
     */
    public Track getTrack(int index) {
        return mPlayerManager.getTrack(index);
    }

    /**
     * 拿到当前播放的playList
     *
     * @return
     */
    public List<Track> getPlayList() {
        return mPlayerManager.getPlayList();
    }

    /**
     * 从list的第startIndex首歌开始播放
     *
     * @param list
     * @param startIndex
     */
    public void playList(List<Track> list, int startIndex) {
        mPlayerManager.playList(list, startIndex);
    }

    /**
     * 从list的第startIndex首歌开始播放
     *
     * @param list
     * @param startIndex
     */
    public void playList(CommonTrackList list, int startIndex) {
        mPlayerManager.playList(list, startIndex);
    }

    /**
     * 设置从list的第startIndex首歌开始播放
     *
     * @param list
     * @param startIndex
     */
    public void setPlayList(List<Track> list, int startIndex) {
        mPlayerManager.setPlayList(list, startIndex);
    }

    /**
     * 设置从list的第startIndex首歌开始播放
     *
     * @param commonTrackList
     * @param startIndex
     */
    public void setPlayList(CommonTrackList commonTrackList, int startIndex) {
        mPlayerManager.setPlayList(commonTrackList, startIndex);
    }

    /**
     * 清除音频缓存
     */
    public void clearPlayCache() {
        mPlayerManager.clearPlayCache();
    }

    /**
     * 拿到音频缓存大小
     *
     * @return
     */
    public static long getPlayCacheSize() {
        return XmPlayerManager.getPlayCacheSize();
    }

    /**
     * 切换专辑列表
     */
    public void switchNextAlbum() {
        playerDataProvider.switchAlbum();
    }

    /**
     * 播放临时的tracklist
     *
     * @param trackList
     */
    public void playTemporaryTracks(TrackList trackList) {
        playerDataProvider.saveCurrentPlayList(mPlayerManager.getCurrentIndex(), mPlayerManager.getPlayCurrPositon());
        playerDataProvider.setCurrentPlayerSourceStatus(PlayerDataProvider.TEMPORARY_SOURCE);
        mPlayerManager.setPlayList(trackList, 0);
    }


}
